﻿package  {
	
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	public class map extends Sprite{
		
		private var Map_data:dataset = new dataset;
		public var tiles:Array = new Array();
		public var spikes:Array = new Array();
		public var flamas:Array = new Array();
		public var powerups:Array = new Array();
		private var level:Array = new Array();
		public var mov_tiles:Array = new Array();
		public var h_x:int;
		public var h_y:int;
		var lvl_sonido:sound;
		
		public var exitDoor:door= new door;

		public function map() {
			// constructor code
			Map_data.Setup();
		}
		
		function crear_map(l:int){
			
			switch(l){
				
				case 1:level = Map_data.level1;lvl_sonido=new sound("sound/bgm_level1.mp3",0,0,-1);break;
				case 2:level = Map_data.level2;lvl_sonido=new sound("sound/bgm_level2v2.mp3",0,0,-1);break;
				case 3:level = Map_data.level3;lvl_sonido=new sound("sound/bgm_level3.mp3",0,0,-1);break;
				case 4:level = Map_data.level4;lvl_sonido=new sound("sound/bgm_level4.mp3",0,0,-1);break;
			
			}
			lvl_sonido.play();
			for(var t = 0; t < level.length; t++){
				for(var u = 0; u < level[t].length; u++){
					if(level[t][u] != 0){									//if the data is not null
						// bloques inverbes
						if ( 0< level[t][u] && level[t][u] <= 10 ) {
							var new_tile:platform_tile = new platform_tile();		//than build a tile
							addChild(new_tile);									//put it on the screen
							
							if(level[t][u]<10)						
							new_tile.gotoAndStop(1);
							//if(level[t][u]<20)
							//new_tile.gotoAndStop(2);
							//if(level[t][u]>20)
							//new_tile.gotoAndStop(3);
							new_tile.x = u * new_tile.width;
							new_tile.y = t * new_tile.height;
							
							tiles.push(new_tile);								//put it in an array
						}
						// exit
						if(level[t][u]==61) {
							//exitDoor:door= new door;
							
							addChild(exitDoor);
							exitDoor.x = u * new_tile.width;
							exitDoor.y = t * new_tile.height;
							
						}
						
						// Spikes
						else if (  10 < level[t][u] && level[t][u]<= 14 ) {
							var spike:Spike;
							spike= new Spike();
							switch(level[t][u]) {
								case 11:
								 	spike.gotoAndStop(1);// R
									break;
								case 12:
									spike.gotoAndStop(2); // L
									break;
								case 13:
									spike.gotoAndStop(3); // U
									break;
								case 14:
									spike.gotoAndStop(4); // D
									break;
							} 
							
							addChild(spike);
							spike.x = u * spike.width;
							spike.y = t * spike.height;
							
							spikes.push(spike);
						}
						else if( 18 < level[t][u] && level[t][u]<= 22){
							
							var new_tile2:platform_tile = new platform_tile();		//than build a tile
							addChild(new_tile2);									//put it on the screen
													
							new_tile2.gotoAndStop(1);
							new_tile2.x = u * new_tile2.width;
							new_tile2.y = t * new_tile2.height;
							
							tiles.push(new_tile2);
							
							var dir = level[t][u]-18;
							var flm:flame_tower;
							flm=new flame_tower(u*32,t*32,dir,1);
							addChild(flm);
							flamas.push(flm);
							
								
							
						}else if( level[t][u]==60){
							h_x=(u*32)+16;
							h_y=(t*32)+16;
						}else if(level[t][u]==65){
							var m_t:m_tile=new m_tile();
							m_t.x=u*32;
							m_t.y=t*32;
							m_t.set_tile(3,2,4,-3);
							
							addChild(m_t);
							mov_tiles.push(m_t)
						}
					}
				}
			}
			//%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
			//LLAMAS.............
			//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			switch(l){
				case 1:
					flamas[0].crear_flama(2); flamas[0].set_time(700);
					flamas[1].crear_flama(2); flamas[1].set_time(700);
					flamas[2].crear_flama(2); flamas[2].set_time(700);
					flamas[3].crear_flama(2); flamas[3].set_time(700);
					flamas[4].crear_flama(8); flamas[4].set_time(1000);
					flamas[5].crear_flama(3); flamas[5].set_time(900);
					flamas[6].crear_flama(3); flamas[6].set_time(900);
					flamas[7].crear_flama(3); flamas[7].set_time(900);
					flamas[8].crear_flama(3); flamas[8].set_time(900);
					flamas[9].crear_flama(5); flamas[9].set_time(1000);
					flamas[10].crear_flama(5); flamas[10].set_time(1000);
					break;
				case 2:
					mov_tiles[0].set_tile(3,8,4,-3);
					mov_tiles[1].set_tile(3,4,4,3);
					mov_tiles[2].set_tile(3,2,4,-1);
					mov_tiles[3].set_tile(3,2,4,2);
					mov_tiles[4].set_tile(3,2,4,-3);
					mov_tiles[5].set_tile(3,2,4,-1);
					mov_tiles[6].set_tile(3,2,4,0);
					mov_tiles[7].set_tile(3,2,4,-3);
					//mov_tiles[1].set_tile(5,3,5,-1);
					flamas[0].crear_flama(5); flamas[0].set_time(700);
					flamas[1].crear_flama(4); flamas[1].set_time(700);
					flamas[2].crear_flama(3); flamas[2].set_time(700);
					flamas[3].crear_flama(3); flamas[3].set_time(700);
					flamas[4].crear_flama(5); flamas[4].set_time(700);
					flamas[5].crear_flama(4); flamas[5].set_time(700);
					flamas[6].crear_flama(4); flamas[6].set_time(700);
					flamas[7].crear_flama(3); flamas[7].set_time(700);
					flamas[8].crear_flama(5); flamas[8].set_time(700);
					break;
				case 3:
					flamas[0].crear_flama(4); flamas[0].set_time(2000);
					flamas[1].crear_flama(5); flamas[1].set_time(2000);
					flamas[2].crear_flama(3); flamas[2].set_time(1700);
					flamas[3].crear_flama(3); flamas[3].set_time(1700);
					flamas[4].crear_flama(3); flamas[4].set_time(1700);
					flamas[5].crear_flama(3); flamas[5].set_time(1700);
					flamas[6].crear_flama(3); flamas[6].set_time(1700);
					flamas[7].crear_flama(3); flamas[7].set_time(1000);
					flamas[8].crear_flama(4); flamas[8].set_time(1600);
					flamas[9].crear_flama(5); flamas[9].set_time(1600);
					flamas[10].crear_flama(5); flamas[10].set_time(1800);
					flamas[11].crear_flama(4); flamas[11].set_time(1800);
					break;
				case 4:
					flamas[0].crear_flama(4); flamas[0].set_time(2000);
					flamas[1].crear_flama(5); flamas[1].set_time(2000);
					flamas[2].crear_flama(3); flamas[2].set_time(2000);
					flamas[3].crear_flama(5); flamas[3].set_time(3000);
					break;
			
			}

			
			
		}//creacion map
	}
	
}
